/**
 * 
 */
package il.ac.bgu.cs.heuristics.graph;

import il.ac.bgu.cs.heuristics.graph.types.IState;
import il.ac.bgu.cs.heuristics.utils.Validation;

import java.util.Set;

class ActionEdgeFactory implements org.jgrapht.EdgeFactory<IState, ActionEdge> {

	private final Set<IState>	preconditions;
	private final Set<IState>	effects;

	ActionEdgeFactory(Set<IState> preconditions, Set<IState> effects) {
		this.preconditions = Validation.ensureArgumentNeverNull(preconditions);
		this.effects = Validation.ensureArgumentNeverNull(effects);
	}

	@Override
	public ActionEdge createEdge(IState from, IState to) {
		Validation.ensureArgumentNeverNull(from);
		Validation.ensureArgumentNeverNull(to);

		boolean isNonContradicting = effects.contains(from)
				&& preconditions.contains(to);

		// if the from node does not belong to precondition or the effect he
		// should be the init state, init state always have nonContradicting
		// edges.
		if (!preconditions.contains(from) && !effects.contains(from))
			isNonContradicting = true;

		return new ActionEdge(from, to, isNonContradicting);
	}

}